import jwt_decode from 'jwt-decode'
import Vue from 'vue'
import VueRouter from 'vue-router'


Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'HomeM',
    // import(/* webpackChunkName: "home" */ '@/pages/HomeM')
    component: (resolve) => require(['@/pages/HomeM'], resolve),
    redirect: '/home',


    children: [
      {
        path: 'home',
        name: 'Home',
        component: (resolve) => require(['@/pages/Home'], resolve)
      },
      {
        path: 'sharp',
        name: 'Sharp',
        component: (resolve) => require(['@/pages/Sharp'], resolve)
      },
      {
        path: 'music',
        name: 'Music',
        component: (resolve) => require(['@/pages/Music'], resolve)
      },
      {
        path: 'game',
        name: 'Game',
        component: (resolve) => require(['@/pages/Game'], resolve)
      },
      {
        path: 'comic',
        name: 'Comic',
        component: (resolve) => require(['@/pages/Comic'], resolve)
      },
      {
        path: 'foods',
        name: 'Foods',
        component: (resolve) => require(['@/pages/Foods'], resolve)
      },

      {
        path: 'upload',
        name: 'Upload',
        component: (resolve) => require(['@/pages/Upload'], resolve)
      },

      {
        path: 'upload/video',
        name: 'UploadVideo',
        component: (resolve) => require(['@/pages/UploadVideo'], resolve)
      },

      {
        path: 'homepage/:id',
        name: 'HomePage',
        component: (resolve) => require(['@/pages/HomePage'], resolve)
      },

      {
        path: 'post',
        name: 'Post',
        component: (resolve) => require(['@/pages/Post'], resolve)
      },

      {
        path: 'post/:id',
        name: 'PostDetail',
        component: (resolve) => require(['@/pages/PostDetail'], resolve)
      },
    ]
  },

  {
    path: '/video/:id',
    name: 'Video',
    component: (resolve) => require(['@/pages/Video'], resolve)
  },

  {
    path: '/whisper/:id?',
    name: 'Whisper',
    component: (resolve) => require(['@/pages/Whisper'], resolve)
  },

  {
    path: '/search',
    name: 'Search',
    component: (resolve) => require(['@/pages/Search'], resolve)
  },

  {
    path: '/test',
    name: 'Test',
    component: (resolve) => require(['@/pages/Test'], resolve)
  },

  {
    path: '/login',
    name: 'Login',
    component: (resolve) => require(['@/pages/Login'], resolve)
  },


  {
    path: '/admin',
    name: 'Admin',
    component: (resolve) => require(['@/pages/Admin'], resolve),

    children: [
      {
        path: 'login',
        name: 'AdminLogin',
        component: (resolve) => require(['@/pages/AdminLogin'], resolve)
      },
      {
        path: 'demo',
        name: 'AdminDemo',
        component: (resolve) => require(['@/pages/AdminDemo'], resolve),

        children: [
          {
            path: 'home',
            name: 'AdminHome',
            component: (resolve) => require(['@/pages/AdminHome'], resolve),
          },
          {
            path: 'video',
            name: 'AdminVideo',
            component: (resolve) => require(['@/pages/AdminVideo'], resolve),
          },
          {
            path: 'complaint',
            name: 'AdminComplaint',
            component: (resolve) => require(['@/pages/AdminComplaint'], resolve),
          }
        ]
      }
    ]
  },
]

const router = new VueRouter(
  {
    routes
  })

router.beforeEach((to, from, next) => {
  let token = localStorage.getItem("token");
  let decoded = null
  let role = null
  if (token) {
    decoded = jwt_decode(token);
    role = decoded.role
  }
  let pattern = /^\/admin\//

  if (to.path === '/login' || to.path === '/admin/login') {
    next();
  } else if (pattern.test(to.path)) {
    if (!token || role != 1) {
      next('/admin/login');
    } else {
      next();
    }
  } else {
    if (!token || role != 0) {
      next('/login');
    } else {
      next();
    }
  }
})

export default router
